package com.renting.modules.taskJob.web;

import com.renting.common.config.HttpStatusConstant;
import com.renting.common.entity.Result;
import com.renting.common.entity.ResultList;
import com.renting.modules.taskJob.entity.Job.impl.NewJob;
import com.renting.modules.taskJob.entity.JobDetail;
import com.renting.modules.taskJob.service.JobService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class JobController {

//    @Autowired
//    JobService jobService;

    @Autowired
    @Qualifier("Scheduler")
    private Scheduler scheduler;
    @Autowired
    private JobService jobService;

    @RequestMapping(value = "v1/sys/jobs", method = RequestMethod.GET)
    @ResponseBody
    @CrossOrigin("*")
    public ResultList<JobDetail> jobs() {
        ResultList<JobDetail> resultList = new ResultList<>();
        List<JobDetail> list = jobService.findAll();
        resultList.setData(list);
        return resultList;
    }


    @RequestMapping(value = "v1/jobs/start")
    @ResponseBody
    @CrossOrigin("*")
    public Result startBlankTimer(JobDetail detail) {
        try {
            scheduler.start();
            org.quartz.JobDetail jobDetail = JobBuilder.newJob(NewJob.class)
                    .withIdentity(detail.getJobName(), detail.getJobGroup()).build();
            CronScheduleBuilder scheduleBuilder =
                    CronScheduleBuilder.cronSchedule(detail.getCron());//"* * * * * ?"
            CronTrigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity(detail.getJobName(), detail.getJobGroup())
                    .withSchedule(scheduleBuilder).build();
            scheduler.scheduleJob(jobDetail, trigger);

            System.out.println("启动"+JobKey.jobKey(detail.getJobName(),detail.getJobGroup()));
        } catch (SchedulerException e) {
            System.out.println(e.getMessage());

        }
        return new Result(HttpStatusConstant.SUCCESS);
    }

    @RequestMapping(value = "v1/jobs/pause")
    @ResponseBody
    @CrossOrigin("*")
    public void pauseDefaultTimer(JobDetail detail) {
        try {
            scheduler.pauseJob(JobKey.jobKey(detail.getJobName(), detail.getJobGroup()));
            System.out.println("停止"+JobKey.jobKey(detail.getJobName(),detail.getJobGroup()));
        } catch (SchedulerException e) {
            System.out.println(e.getMessage());
        }
    }

    @RequestMapping(value = "v1/jobs/resume")
    @ResponseBody
    @CrossOrigin("*")
    public void resume(JobDetail detail) {
        try {
            scheduler.resumeJob(JobKey.jobKey(detail.getJobName(), detail.getJobGroup()));
            System.out.println("恢复"+JobKey.jobKey(detail.getJobName(),detail.getJobGroup()));
        } catch (SchedulerException e) {
        }
    }



}
